<template>
  <div>
    <!-- :city 父组件通过属性 传递数据给子组件-->
    <home-header></home-header>
    <home-swiper :list="swiperList"></home-swiper>
    <home-icons :icons="iconList"></home-icons>
    <home-recommend :recommendList="recommendList"></home-recommend>
    <home-weekend :weekendList="weekendList"></home-weekend>
  </div>
</template>

<script>
import HomeHeader from './components/Header'
import HomeSwiper from './components/Swiper'
import HomeIcons from './components/Icons'
import HomeRecommend from './components/Recommend'
import HomeWeekend from './components/Weekend'
import axios from 'axios'
import { mapState } from 'vuex'
export default {
  name: 'Home',
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    HomeWeekend
  },
  data () {
    return {
      // city: '', // 存放城市
      lastCity: '',
      swiperList: [],
      iconList: [],
      recommendList: [],
      weekendList: []
    }
  },
  computed: {
    ...mapState(['city'])
  },
  methods: {
    getHomeInfo () {
      axios.get('/api/index.json?city=' + this.city) // 获取数据，返回一个Promise对象
        .then(this.getHomeInfoSucc) // 数据获取成功后执行
    },
    getHomeInfoSucc (res) {
      res = res.data
      if (res.ret && res.data) {
        var data = res.data
        // this.city = data.city // 加载城市数据
        this.swiperList = data.swiperList // 加载幻灯片数据
        this.iconList = data.iconList // icons列表去
        this.recommendList = data.recommendList
        this.weekendList = data.weekendList
      }
      // console.log(res)
    }
  },
  mounted () { // 页面挂载好后执行函数
    this.lastCity = this.city // 对上一次的城市保存
    this.getHomeInfo()
    // console.log('mounted')
  },
  activated () { // 使用keepalive后新增的生命周期函数
    // console.log('activated')
    if (this.lastCity !== this.city) {
      this.lastCity = this.city
      this.getHomeInfo()
    }
  }
}
</script>

<style></style>
